﻿using System;
using System.Globalization;
using Business;
using Entities;
namespace ServicesDirectory.UserControl
{
    public partial class ServiceFunding : System.Web.UI.UserControl
    {

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            txtFundingStart.TextChanged += TxtFundingStart_TextChanged;
            txtFundingEnd.TextChanged += TxtFundingEnd_TextChanged;
            txtFundraisingCompleteDate.TextChanged += TxtFundraisingCompleteDate_TextChanged;
            txtFundingDonorDate.TextChanged += TxtFundingDonorDate_TextChanged;
            txtFundraisingRequiredBy.TextChanged += TxtFundraisingRequiredBy_TextChanged;
            txtFundingStart.Attributes.Add("onclick", "displayCalendar(document.forms[0].ctl00_cphContain_ServiceDetail1_ServiceFunding1_txtFundingStart,'dd/mm/yyyy',this)");
            txtFundingEnd.Attributes.Add("onclick", "displayCalendar(document.forms[0].ctl00_cphContain_ServiceDetail1_ServiceFunding1_txtFundingEnd,'dd/mm/yyyy',this)");
            txtFundraisingCompleteDate.Attributes.Add("onclick", "displayCalendar(document.forms[0].ctl00_cphContain_ServiceDetail1_ServiceFunding1_txtFundraisingCompleteDate,'dd/mm/yyyy',this)");
            txtFundraisingRequiredBy.Attributes.Add("onclick", "displayCalendar(document.forms[0].ctl00_cphContain_ServiceDetail1_ServiceFunding1_txtFundraisingRequiredBy,'dd/mm/yyyy',this)");
            txtFundingDonorDate.Attributes.Add("onclick", "displayCalendar(document.forms[0].ctl00_cphContain_ServiceDetail1_ServiceFunding1_txtFundingDonorDate,'dd/mm/yyyy',this)");
            txtFundingNeeds.Attributes.Add("onKeyPress", "return(currencyFormat(this,',','.',event))");
            txtFundraisingAmount.Attributes.Add("onKeyPress", "return(currencyFormat(this,',','.',event))");
            txtFundraisingNeeds.Attributes.Add("onKeyPress", "return(currencyFormat(this,',','.',event))");
            txtAmount.Attributes.Add("onKeyPress", "return(currencyFormat(this,',','.',event))");
            ListContact1.GetContactId += ListContact1_GetContactId;
            ListContact1.CloseControl += ListContact1_CloseControl;
            lbtLookupContact.Click += LbtLookupContact_Click;
            chkFundraisingComplete.CheckedChanged += ChkFundraisingComplete_CheckedChanged;
            chkContiniuationNeeded.CheckedChanged += ChkContiniuationNeeded_CheckedChanged;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the ChkContiniuationNeeded control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ChkContiniuationNeeded_CheckedChanged(object sender, EventArgs e)
        {
            txtFundingContiniuationAmount.Enabled = chkContiniuationNeeded.Checked;
            txtContiniuationDetails.Enabled = chkContiniuationNeeded.Checked;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the ChkFundraisingComplete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ChkFundraisingComplete_CheckedChanged(object sender, EventArgs e)
        {

            imgFundraisingCompleteDate.Visible = chkFundraisingComplete.Checked;
            imgFundraisingCompleteDate1.Visible = !chkFundraisingComplete.Checked;
            txtFundraisingCompleteDate.Enabled = chkFundraisingComplete.Checked;
        }
        /// <summary>
        /// Loads the reference data.
        /// </summary>
        public void LoadReferenceData()
        {
            ddlFundingsource.DataSource = ServiceBusiness.Instance.GetFundingSource();
            ddlFundingsource.DataValueField = "RefValue";
            ddlFundingsource.DataTextField = "DataRef";
            ddlFundingsource.DataBind();
        }

        void LbtLookupContact_Click(object sender, EventArgs e)
        {
            pnFundingContact.Visible = true;
        }

        /// <summary>
        /// Handles the CloseControl event of the ListContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ListContact1_CloseControl(object sender, EventArgs e)
        {
            pnFundingContact.Visible = false;
        }

        /// <summary>
        /// Handles the GetContactId event of the ListContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ListContact1_GetContactId(object sender, EventArgs e)
        {
            var contact = sender as Contact;
            if (contact != null)
            {
                lblLookupDetail.Text = contact.FirstName + " " + contact.SurName;
                lbtLookupContact.CommandArgument = contact.ContactId.ToString();
            }
            else
            {
                lblLookupDetail.Text = string.Empty;
                lbtLookupContact.CommandArgument = string.Empty;
            }
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtFundraisingRequiredBy control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtFundraisingRequiredBy_TextChanged(object sender, EventArgs e)
        {
            DateTime fundraisingCompleteDate;
            if (DateTime.TryParseExact(txtFundraisingRequiredBy.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundraisingCompleteDate) == false)
            {
                txtFundraisingRequiredBy.Text = "";
                txtFundraisingRequiredBy.Focus();
            }
            else
            {
                txtFundraisingRequiredBy.Text = fundraisingCompleteDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtFundingDonorDate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtFundingDonorDate_TextChanged(object sender, EventArgs e)
        {
            DateTime fundraisingCompleteDate;
            if (DateTime.TryParseExact(txtFundingDonorDate.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundraisingCompleteDate) == false)
            {
                txtFundingDonorDate.Text = "";
                txtFundingDonorDate.Focus();
            }
            else
            {
                txtFundingDonorDate.Text = fundraisingCompleteDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
        }
        /// <summary>
        /// Handles the TextChanged event of the TxtFundraisingCompleteDate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtFundraisingCompleteDate_TextChanged(object sender, EventArgs e)
        {
            if (chkFundraisingComplete.Checked)
            {
                DateTime fundraisingCompleteDate;
                if (DateTime.TryParseExact(txtFundraisingCompleteDate.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundraisingCompleteDate) == false)
                {
                    txtFundraisingCompleteDate.Text = "";
                    txtFundraisingCompleteDate.Focus();
                }
                else
                {
                    txtFundraisingCompleteDate.Text = fundraisingCompleteDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
                }
            }
            else
            {
                txtFundraisingCompleteDate.Text = "";
            }
        }
        /// <summary>
        /// Handles the TextChanged event of the TxtFundingEnd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtFundingEnd_TextChanged(object sender, EventArgs e)
        {
            DateTime fundingStart;
            if (DateTime.TryParseExact(txtFundingEnd.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundingStart) == false)
            {
                txtFundingEnd.Text = "";
                txtFundingEnd.Focus();
            }
            else
            {
                txtFundingEnd.Text = fundingStart.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtFundingStart control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtFundingStart_TextChanged(object sender, EventArgs e)
        {
            DateTime fundingStart;
            if (DateTime.TryParseExact(txtFundingStart.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundingStart) == false)
            {
                txtFundingStart.Text = "";
                txtFundingStart.Focus();
            }
            else
            {
                txtFundingStart.Text = fundingStart.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            }

        }

    }
}